/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.wx.startup;

import cn.ac.iie.di.wx.common.RuntimeEnv;
import cn.ac.iie.di.wx.configuration.Configuration;
import cn.ac.iie.di.wx.httpservice.HttpGetter;
import static cn.ac.iie.di.wx.startup.Starter.logger;
import cn.ac.iie.di.wx.utils.DateUtil;
import cn.ac.iie.di.wx.utils.RedisUtil;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.apache.log4j.Logger;

/**
 *
 * @author Administrator
 */
public class CplAll {

    static Logger logger = Logger.getLogger(CplAll.class.getName());

    public static void init() throws Exception {
        String configurationFileName = "serverport.properties";
        Configuration conf = Configuration.getConfiguration(ClassLoader.getSystemClassLoader().getResource(configurationFileName).getPath());
        if (conf == null) {
            throw new Exception("reading " + configurationFileName + " is failed.");
        }
        try {
            RuntimeEnv.initialize(conf);///参数，
        } catch (Exception ex) {
            throw new Exception("initializng runtime enviroment is failed for" + ex.getMessage());
        }
    }

    public static void main(String[] args) {
        try {
            init();
            HttpGetter httpGetter = new HttpGetter();

            String redisHosts = ((String) RuntimeEnv.getParam(RuntimeEnv.REDIS_HOSTS));
            String masterName = (String) RuntimeEnv.getParam(RuntimeEnv.REDIS_MASTER_NAME);
            String dbName = (String) RuntimeEnv.getParam(RuntimeEnv.REDIS_DB_NAME);

            String user = (String) RuntimeEnv.getParam(RuntimeEnv.USER);
            String password = (String) RuntimeEnv.getParam(RuntimeEnv.PASSWORD);
            RedisUtil userRedisUtil = new RedisUtil(redisHosts, masterName, Integer.parseInt(dbName));
            logger.info("init redis:" + redisHosts + "\t" + masterName + "\t" + dbName + "\tuse:" + user + "-" + password + "at " + DateUtil.getToDayDate());
            //从今日往前推
            //设置最前时间
            String lastDateStr = (String) RuntimeEnv.getParam(RuntimeEnv.DATE);
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
            String todayDate = df.format(new Date());
            logger.info(lastDateStr + "-->" + todayDate);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Date lastDate = sdf.parse(lastDateStr);
            logger.info(lastDate.before(sdf.parse("20170508")));
            int count = 0;
            while (lastDate.before(sdf.parse(DateUtil.getBeforeDate(count)))) {
                String yesterdayDate = DateUtil.getBeforeDate(count - 1);
                String TheDayBeforeDate = DateUtil.getBeforeDate(count - 2);
                logger.info("前" + count + "天数据补全" + "yesterdayDate = " + yesterdayDate + "TheDayBeforeDate = " + TheDayBeforeDate);
                String yesterdayDataSize = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.DATASIZE) + yesterdayDate);//dataSize
                String theDayBeforeDataSize = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.DATASIZE) + TheDayBeforeDate);
                String allCount = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.ALLCOUNT) + yesterdayDate);
                String successCount = userRedisUtil.get((String) RuntimeEnv.getParam(RuntimeEnv.SUCCESSCOUNT) + yesterdayDate);

                // 当天用户和昨天用户信息是否相同的一个估量， 0：不同; 10：相同,用当天的datasize减去昨天的
                String evaluate;
                logger.info((String) RuntimeEnv.getParam(RuntimeEnv.DATASIZE) + yesterdayDate + "=" + yesterdayDataSize);
                logger.info((String) RuntimeEnv.getParam(RuntimeEnv.DATASIZE) + TheDayBeforeDate + "=" + theDayBeforeDataSize);
                logger.info((String) RuntimeEnv.getParam(RuntimeEnv.ALLCOUNT) + yesterdayDate + "=" + allCount);
                logger.info((String) RuntimeEnv.getParam(RuntimeEnv.SUCCESSCOUNT) + yesterdayDate + "=" + successCount);
                if (yesterdayDataSize != null && allCount != null && successCount != null && theDayBeforeDataSize != null) {
                    if (yesterdayDataSize.equals(theDayBeforeDataSize)) {
                        evaluate = "10";
                    } else {
                        evaluate = "0";
                    }
                    logger.info("yesterdayDataSize=" + yesterdayDataSize + "theDayBeforeDataSize=" + theDayBeforeDataSize + "allCount=" + allCount + "successCount=" + successCount + "evaluate=" + evaluate);
                    String userrequest = "user=" + user + "&password=" + password + "&date=" + yesterdayDate
                            + "&dataSize=" + yesterdayDataSize + "&allCount=" + allCount + "&successCount=" + successCount + "&evaluate=" + evaluate;
                    String user_url = (String) RuntimeEnv.getParam(RuntimeEnv.USER_URL) + "?";
                    httpGetter.doGetByDate(user_url + userrequest, DateUtil.getBeforeDate(count));
                } else {
                    logger.warn("can not get user datas from redis");
                }
                count--;
            }
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }

    }

}
